package com.dayaokeji.rhythmschoolstudent.client.common.exam.adapter;

import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.RadioButton;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.util.MultiTypeDelegate;
import com.dayaokeji.rhythmschoolstudent.R;
import com.dayaokeji.rhythmschoolstudent.client.common.exam.subject_type.utils.ScoreUtil;
import com.dayaokeji.rhythmschoolstudent.client.common.exam.subject_type.utils.SubjectTypeUtil;
import com.dayaokeji.rhythmschoolstudent.client.home.course.task.FilesAdapter;
import com.dayaokeji.rhythmschoolstudent.utils.ExamUtils;
import com.dayaokeji.rhythmschoolstudent.wiget.WrapLinearLayoutManager;
import com.dayaokeji.server_api.domain.ExamQuestion;
import com.dayaokeji.server_api.domain.SubjectAnswer;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * ********************************************************************
 * *  Created by Zeno on 2018/7/10
 * ********************************************************************
 * *  version 1.0
 * ********************************************************************
 * *  Copyright (C) dayaokeji.com 2018 -- 2020 All rights reserved.
 * ********************************************************************
 * *
 * ********************************************************************
 */
public class ExamQuestionListAdapter extends BaseQuickAdapter<ExamQuestion,BaseViewHolder> {

    public ExamQuestionListAdapter(@Nullable List<ExamQuestion> data) {
        super(data);

        setMultiTypeDelegate(new MultiTypeDelegate<ExamQuestion>() {
            @Override
            protected int getItemType(ExamQuestion examQuestion) {
                return examQuestion.getType();
            }
        });
        getMultiTypeDelegate().registerItemType(SubjectTypeUtil.SINGLE_CHOOSE, R.layout.exam_choose_list_layout);
        getMultiTypeDelegate().registerItemType(SubjectTypeUtil.MULTIPLE_CHOOSE, R.layout.exam_choose_list_layout);
        getMultiTypeDelegate().registerItemType(SubjectTypeUtil.QUESTION_AND_ANSWER, R.layout.fragment_question_and_answer);
        getMultiTypeDelegate().registerItemType(SubjectTypeUtil.BLANK_SUBJECT, R.layout.fragment_blank_subject);
        getMultiTypeDelegate().registerItemType(SubjectTypeUtil.JUDGMENT_PROBLEM, R.layout.fragment_judgment_problem);
    }

    @Override
    protected void convert(BaseViewHolder helper, ExamQuestion item) {
        helper.setText(R.id.tv_subject_type, ExamUtils.getSubjectTypeStr(item.getType()));
        helper.setText(R.id.tv_subject_score, String.format("分值：%s 分", ScoreUtil.doubleTrans(item.getScore())));
        helper.setText(R.id.tv_subject_content, item.getContent());
        RecyclerView rvSubjectImages = helper.getView(R.id.rv_subject_imgs);
        final SubjectImgAdapter subjectImgAdapter = new SubjectImgAdapter();
        subjectImgAdapter.setNewData(item.getResourceList());
        rvSubjectImages.setLayoutManager(new GridLayoutManager(mContext,4));
        rvSubjectImages.setAdapter(subjectImgAdapter);
        SubjectAnswer subjectAnswer = item.getSubjectAnswer();
        if (subjectAnswer == null) subjectAnswer = new SubjectAnswer();
        switch (helper.getItemViewType()) {
            case SubjectTypeUtil.SINGLE_CHOOSE:
                ChooseSubject(helper, item,subjectAnswer);
                break;
            case SubjectTypeUtil.MULTIPLE_CHOOSE:
                ChooseSubject(helper, item,subjectAnswer);
                break;
            case SubjectTypeUtil.QUESTION_AND_ANSWER:
                questionAndAnswer(helper, item,subjectAnswer);
                break;
            case SubjectTypeUtil.BLANK_SUBJECT:
                blankSubject(helper,item, subjectAnswer);
                break;
            case SubjectTypeUtil.JUDGMENT_PROBLEM:
                judgmentProblem(helper,item, subjectAnswer);
                break;
        }
    }

    private void ChooseSubject(BaseViewHolder helper, ExamQuestion item,SubjectAnswer subjectAnswer) {

        if (TextUtils.isEmpty(subjectAnswer.getAnswer())) {
            final String studentAnswer = item.getStudentAnswer();
            if (!TextUtils.isEmpty(item.getCorrectAnswer())){
                subjectAnswer.setStandardAnswer(item.getCorrectAnswer());
            }
            if (item.getType() == SubjectTypeUtil.SINGLE_CHOOSE) {
                subjectAnswer.setAnswer(studentAnswer);
            }else{
                if (!TextUtils.isEmpty(studentAnswer)) {
                    subjectAnswer.setAnswer(studentAnswer);
                    if (studentAnswer.contains(",")) {
                        final String[] strings = studentAnswer.split(",");
                        final List<String> answers = new ArrayList<>();
                        for (String string : strings) {
                            answers.add(string);
                        }
                        subjectAnswer.setChooseIndex(answers);
                    }else{
                        subjectAnswer.setChooseIndex(Arrays.asList(studentAnswer));
                    }
                }
            }
        }

        RecyclerView rvSubjectChooseItems = helper.getView(R.id.rv_subject_choose_items);
        final SubjectChooseItemAdapter chooseItemAdapter = new SubjectChooseItemAdapter(subjectAnswer, item.getType(), true,false);
        chooseItemAdapter.setNewData(item.getChoiceInfoList());
        final WrapLinearLayoutManager wrapLinearLayoutManager = new WrapLinearLayoutManager(mContext);
        rvSubjectChooseItems.setLayoutManager(wrapLinearLayoutManager);
        rvSubjectChooseItems.setAdapter(chooseItemAdapter);
    }

    private void questionAndAnswer(BaseViewHolder helper, ExamQuestion item,SubjectAnswer subjectAnswer) {
        EditText editText = helper.getView(R.id.et_question_and_answer);
        RecyclerView rvSubjectAnswerPic = helper.getView(R.id.rv_subject_answer_pic);
        editText.setEnabled(false);
        editText.setText(subjectAnswer.getAnswer());
        if (!TextUtils.isEmpty(item.getStudentAnswer()) && TextUtils.isEmpty(subjectAnswer.getAnswer())) {
            editText.setText(item.getStudentAnswer());
        }
        final FilesAdapter filesAdapter = new FilesAdapter();
        rvSubjectAnswerPic.setLayoutManager(new GridLayoutManager(mContext,4));
        rvSubjectAnswerPic.setAdapter(filesAdapter);
        if (!subjectAnswer.getResourceList().isEmpty()) {
            List<FilesAdapter.FileBean> fileBeans = new ArrayList<>();
            for (Integer integer : subjectAnswer.getResourceList()) {
                FilesAdapter.FileBean fileBean = new FilesAdapter.FileBean();
                fileBean.setFileRes(integer);
                fileBeans.add(fileBean);
            }
            filesAdapter.setNewData(fileBeans);
        }
    }

    private void blankSubject(BaseViewHolder helper, ExamQuestion item,SubjectAnswer subjectAnswer) {
       EditText etBlankSubject = helper.getView(R.id.et_blank_answer);
       etBlankSubject.setEnabled(false);
       etBlankSubject.setText(subjectAnswer.getAnswer());
        if (!TextUtils.isEmpty(item.getStudentAnswer()) && TextUtils.isEmpty(subjectAnswer.getAnswer())) {
            etBlankSubject.setText(item.getStudentAnswer());
        }
    }

    private void judgmentProblem(BaseViewHolder helper, ExamQuestion item,SubjectAnswer subjectAnswer) {
        RadioButton rbAnswerSuccess = helper.getView(R.id.rb_answer_success);
        RadioButton rbAnswerError = helper.getView(R.id.rb_answer_error);

        rbAnswerSuccess.setEnabled(false);
        rbAnswerError.setEnabled(false);

        String answer = "";
        if (TextUtils.isEmpty(subjectAnswer.getAnswer())) {
            answer = item.getStudentAnswer();
        }

        if (!TextUtils.isEmpty(answer)) {
            if ("正确".equals(subjectAnswer.getAnswer()) || "F".equals(subjectAnswer.getAnswer()) || "对".equals(subjectAnswer.getAnswer())) {
                rbAnswerSuccess.setChecked(true);
            }else{
                rbAnswerError.setChecked(true);
            }
        }
    }

}
